package com.example.wordlistdemo;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wordlistdemo.dao.DatabaseHelper;

import java.util.ArrayList;
import java.util.List;


    public class WordAdapter extends RecyclerView.Adapter<WordAdapter.ViewHolder> {
        private DatabaseHelper databaseHelper;

        private List<WordEntity> wordList;


        private Context context;
        private static String STARRED_STR = "****************";

        private static final int TYPE_ITEM = 0;
        private static final int TYPE_SEPARATOR = 1;

        public WordAdapter(Context context, List<WordEntity> wordList) {
            this.context = context;
            this.wordList = wordList;
            databaseHelper = new DatabaseHelper(context);
        }


        @Override
        public int getItemViewType(int position) {
            // 每隔20个项返回分隔符的视图类型
            if (position % 20 == 0) {
                return TYPE_SEPARATOR;
            } else {
                return TYPE_ITEM;
            }
        }


        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                // 创建列表项的视图持有者
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
                view.setVerticalScrollBarEnabled(false);
                return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            WordEntity word = wordList.get(position);
            holder.tvNumber.setText(String.valueOf(position+1));
            holder.tvEnglish.setText(word.getEnglish());
           // holder.tvChinese.setText(word.getChinese());
            holder.tvPhonetic.setText(word.getPhonetic());

            holder.tvChinese.setText(wordList.get(position).isHide() ? STARRED_STR : wordList.get(position).getChinese()); // 根据状态设置文本
            holder.tvChinese.setOnClickListener(v -> {

                holder.tvChinese.setText(wordList.get(position).isHide() ? wordList.get(position).getChinese():STARRED_STR); // 根据状态设置文本
                // 切换隐藏状态
                int newIsHide = word.isHide() ? 0 :1;
                updateIsHide(word.getEnglish(),newIsHide);

                wordList.get(position).setIsHide(newIsHide);
                notifyItemRangeChanged(position, wordList.size());

            });


            // 根据 "favorite" 属性设置收藏按钮的状态
            holder.btnFavorite.setImageResource(word.isFamiliar()?R.drawable.biz_wg_icon_radio_selected:R.drawable.biz_wg_icon_radio_not_selected);
            // 设置收藏按钮的点击事件
            holder.btnFavorite.setOnClickListener(v -> {

                wordList.remove(position);
                notifyItemRemoved(position);
                // 刷新列表项
                notifyItemRangeChanged(position, wordList.size());
                // 更新数据库中的收藏状态，这里你需要调用相应的数据库操作方法
                // 切换收藏状态
                int newFavoriteStatus = word.isFamiliar() ? 0 : 1;
                updateFamiliarityInDatabase(word.getEnglish(),newFavoriteStatus);
                //更新bar
                MainActivity mainActivity = (MainActivity) context;
                mainActivity.updateActionBarTitle(wordList.size());
            });

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 处理单词点击事件，跳转到DetailActivity并展示谷歌首页
                    Context context = v.getContext();
                    Intent intent = new Intent(context, WordDetailActivity.class);
                    intent.putExtra("english", word.getEnglish());
                    context.startActivity(intent);
                }
            });
        }


        @Override
        public int getItemCount() {
            return wordList.size();
        }

        public static class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvNumber;
            TextView tvEnglish;
            TextView tvChinese;
            TextView tvPhonetic;
            ImageButton btnFavorite;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                tvNumber = itemView.findViewById(R.id.textViewNumber);
                tvEnglish = itemView.findViewById(R.id.textViewEnglish);
                tvChinese = itemView.findViewById(R.id.textViewChinese);
                tvPhonetic = itemView.findViewById(R.id.textViewPhonetic);
                btnFavorite = itemView.findViewById(R.id.btnFavorite);
            }
        }

        private void updateFamiliarityInDatabase(String englishWord, int isFamiliar) {
            databaseHelper.updateFamiliarity(englishWord, isFamiliar);
        }

        private void updateIsHide(String englishWord, int isHide) {
            databaseHelper.updateIsHide(englishWord, isHide);
        }


}

